<?php
namespace app\backend\controllers;

use app\backend\common\ViewBaseController;
use lib\helper\error;
use lib\redis\RedisTool;
use lib\rpc\RpcClient;
use lib\Tool\Tools;

class LoginController extends ViewBaseController {

    /**
     * 登录
     * @return false|string
     */
    public function actionIndex(){
        $post = Tools::My()->request()->post()?:[];
        $userType = RedisTool::_init()->setKey('USER_LOGIN')->hGet(session_id()?:'ERROR_LOGIN_'.rand(100,999))->getReturn();
        if (!empty($userType)){
            Header("Location: /app/backend/index.php");
        }
        if (!empty($post)){
            $cipher = substr($post['pass_word'],-2);
            $serverCipher = substr(date('H:i'),-2);
            if ($cipher != $serverCipher)error::_plainError(['message'=>'密钥错误']);
            $data =  RpcClient::find('UserController')->getUserLoginByUserPass($post['user_name'],md5(substr($post['pass_word'],0,-2)),1)?:[];
            RedisTool::_init()->setKey('USER_LOGIN')->hSet(session_id()?:'ERROR_LOGIN_'.rand(100,999),json_encode($data))->setTime(604800);
            Header("Location: /app/backend/index.php");
        }

        return $this->display('login/index','login');
    }

    /**
     * 退出
     */
    public function actionOutBackEnd(){
        RedisTool::_init()->setKey('USER_LOGIN')->hSet(session_id()?:'ERROR_LOGIN_'.rand(100,999),'');
        Header("Location: /app/backend/index.php");
    }

}
